﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_DonDatHang : System.Web.UI.Page
{
    DADataContext da = new DADataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var mcn = (from cn in da.Chucnangs where cn.Tenchucnang == "Quản lý đơn đặt hàng" select cn.Machucnang).FirstOrDefault();
            var kt = (from a in da.TaikhoanChucnangs where (a.Taikhoan == Session["user"].ToString() && a.Machucnang == mcn) select a).Count();
            if (kt == 1)
            {
                gvDonDatHang.DataSource = from x in da.DatHangs orderby x.NgayDatHang descending select x;
                gvDonDatHang.DataBind();
            }
            else
            {
                Response.Redirect("/Online/Admin/Default.aspx");
            }
        }
    }
    protected void gvDonDatHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Select"))
        {
            string id = e.CommandArgument.ToString();
            gvChiTiet.DataSource = from a in da.ChiTietDHs
                                   from b in da.Sanphams
                                   where a.IDSanPham.Equals(b.Masp) && a.IDDatHang.Equals(id)
                                   select new { Tensp = b.Tensp, SoLuong = a.SoLuong };
            gvChiTiet.DataBind();

            fChiTiet.Visible = true;
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                string ids = e.CommandArgument.ToString();
                var delchitiet = from y in da.ChiTietDHs where y.IDDatHang.Equals(ids) select y;
                da.ChiTietDHs.DeleteAllOnSubmit(delchitiet);

                var del = (from x in da.DatHangs where x.ID.Equals(ids) select x).FirstOrDefault();
                da.DatHangs.DeleteOnSubmit(del);
                da.SubmitChanges();

                gvDonDatHang.DataSource = from x in da.DatHangs orderby x.NgayDatHang descending select x;
                gvDonDatHang.DataBind();
            }
    }
}